import sys
import random

import pygame

from constants import SCREEN_WIDTH, SCREEN_HEIGHT, FPS, BLACK
from utils import load_image, draw_text, draw_health_bar
from core.player import Player
from core.enemy import OrdinaryEnemy, FastEnemy, BossEnemy, HeavyEnemy, EliteEnemy, SuperEnemy
from core.bullet import Bullet
from core.explosion import Explosion


# 初始化 Pygame
pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("aircraft-war")

PLAYER_IMG = load_image("./resource/player.svg", 1.0)
ORDINARY_ENEMY_IMG = load_image("./resource/ordinary_enemy.svg", 1.0)
FAST_ENEMY_IMG = load_image("./resource/fast_enemy.svg", 1.0)
HEAVY_ENEMY_IMG = load_image("./resource/heavy_enemy.svg", 2.0)
ELITE_ENEMY_IMG = load_image("./resource/elite_enemy.svg", 1.0)
SUPER_ENEMY_IMG = load_image("./resource/super_enemy.svg", 2.0)
BOSS_ENEMY_IMG = load_image("./resource/boss_enemy.svg", 2.0)
BULLET_IMG = load_image("./resource/bullet.svg", 1.0)
EXPLOSION_IMG = load_image("./resource/explosion.svg", 1.0)

background = pygame.image.load("./resource/background.jpg").convert()
# 调整图片大小以适应屏幕（可选）
background = pygame.transform.scale(background, (SCREEN_WIDTH, SCREEN_HEIGHT))

# 加载音效
BULLET_SOUND = pygame.mixer.Sound("./resource/sound.wav")

# 创建精灵组
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()

player = Player(PLAYER_IMG)
all_sprites.add(player)

# 游戏主循环参数
running = True
clock = pygame.time.Clock()
score = 0
font = pygame.font.SysFont("simhei", 30)  # 使用黑体字体显示中文
last_enemy = pygame.time.get_ticks()
enemy_delay = 1000  # 每秒生成一个敌机
has_boss = False
last_bullet = last_enemy
bullet_delay = 500

# 主循环
while running:
    clock.tick(FPS)
    now = pygame.time.get_ticks()

    # 事件监听
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # 自动发射子弹（每隔 BULLET_COOLDOWN 毫秒发射一次）
    if now - last_bullet > bullet_delay:
        bullet = Bullet(BULLET_IMG, player.rect.centerx, player.rect.top)
        all_sprites.add(bullet)
        bullets.add(bullet)
        BULLET_SOUND.play()
        last_bullet = now  # 更新上次发射时间

    if not has_boss:
        # 生成boss
        if score > 0 and score % 100 == 0:
            enemy = BossEnemy(BOSS_ENEMY_IMG)
            all_sprites.add(enemy)
            enemies.add(enemy)
            has_boss = True

        # 生成敌人
        elif now - last_enemy > enemy_delay:
            rand = random.randint(1, 10)
            if rand <= 1:
                enemy = EliteEnemy(FAST_ENEMY_IMG)
            elif rand <= 3:
                enemy = FastEnemy(ELITE_ENEMY_IMG)
            elif rand <= 5:
                enemy = HeavyEnemy(HEAVY_ENEMY_IMG)
            elif rand <= 6:
                enemy = SuperEnemy(SUPER_ENEMY_IMG)
            else:
                enemy = OrdinaryEnemy(ORDINARY_ENEMY_IMG)
            all_sprites.add(enemy)
            enemies.add(enemy)
            last_enemy = now

    # 更新逻辑
    keys = pygame.key.get_pressed()
    all_sprites.update(keys)

    # 子弹击中敌机
    hits = pygame.sprite.groupcollide(enemies, bullets, False, True)
    for enemy, bullet_list  in hits.items():
        for bullet in bullet_list:
            enemy.health -= 10  # 每颗子弹减少10点血量
            if enemy.health <= 0:
                enemy.kill()  # 血量<=0时才移除敌机
                score += 1
                explosion = Explosion(EXPLOSION_IMG, enemy.rect.center)
                all_sprites.add(explosion)

    # 敌机撞到玩家
    enemy_hits = pygame.sprite.spritecollide(player, enemies, False)
    for enemy in enemy_hits:
        player.health -= 20  # 每次撞击减少20点血量
        enemy.health = 0  # 撞击玩家的敌机立即销毁
        enemy.kill()
        explosion = Explosion(EXPLOSION_IMG, enemy.rect.center)
        all_sprites.add(explosion)

        if player.health <= 0:  # 玩家血量<=0才结束游戏
            running = False

    # 绘制画面
    # screen.fill(BLACK)
    # 绘制背景
    screen.blit(background, (0, 0))
    all_sprites.draw(screen)
    draw_health_bar(screen, 10, 10, player.health, player.max_health)
    draw_text(screen, f"得分: {score}", font, 30, 360, 10)
    pygame.display.flip()

# 结束界面
draw_text(screen, "游戏结束", font, 40, SCREEN_WIDTH // 2 - 60, SCREEN_HEIGHT // 2)
pygame.display.flip()
pygame.time.wait(2000)

pygame.quit()
sys.exit()
